package com.example.home.loveDonation.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.loveDonation.bean.BenefitBean;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;
import com.example.home.tool.RunOnUiThreadTool;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class BenefitApter extends RecyclerView.Adapter<BenefitApter.MyHoldy> {
    private Context context;
    private List<BenefitBean.RowsBean> list;
    private View view;

    public BenefitApter(Context context, List<BenefitBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item_ben, null);
        return new MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        Glide.with(context).load(ip + list.get(position).getImgUrl()).into(holder.imgUrl);
        holder.name.setText("项目类型:" + list.get(position).getName());
        holder.type_name.setText("公益类别:" + list.get(position).getType().getName());
        holder.author.setText("发布人:" + list.get(position).getAuthor());
        holder.moneyNow.setText("已筹善款:" + list.get(position).getMoneyNow());
        holder.donateCount.setText("参捐人数:" + list.get(position).getDonateCount());
        holder.activityAt.setText("项目时间:" + list.get(position).getActivityAt());
        holder.donation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showInputDialog();
            }
        });
    }

    private void showInputDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请输入想要的捐款金额");

        // 添加一个EditText到对话框中
        final EditText input = new EditText(context);
        builder.setView(input);

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String userInput = input.getText().toString();
                double donateAmount = Double.parseDouble(userInput);

                if (donateAmount == 0) {
                    Toast.makeText(context, "捐款金额不能为0，请重新输入", Toast.LENGTH_SHORT).show();
                } else {
                    // 发送网络请求
                    OKHttpTool.doNetRequest("prod-api/api/public-welfare/donate-record",
                            OKHttpTool.POST,
                            GeneralClass.class, // 将GeneralClass对象作为请求体发送
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    GeneralClass generalClass = (GeneralClass) obj;
                                    RunOnUiThreadTool.run((Activity) context, new RunOnUiThreadTool.RunCallback() {
                                        @Override
                                        public void set() {
                                            generalClass.setDonateMoney(userInput);
                                            Log.e("xhf", "捐款金额=" + generalClass.getDonateMoney());
                                            Toast.makeText(context, "捐款成功", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {
                                    Log.e("xhf", "网络访问失败");
                                }
                            }
                    );
                }
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }


        @Override
        public int getItemCount () {
            return list == null ? 0 : list.size();
        }

        public class MyHoldy extends RecyclerView.ViewHolder {
            ImageView imgUrl;
            TextView name;
            TextView type_name;
            TextView author;
            TextView moneyNow;
            TextView donateCount;
            TextView activityAt;
            Button donation;

            public MyHoldy(@NonNull View itemView) {
                super(itemView);
                imgUrl = itemView.findViewById(R.id.imgUrl);
                name = itemView.findViewById(R.id.name);
                type_name = itemView.findViewById(R.id.type_name);
                author = itemView.findViewById(R.id.author);
                moneyNow = itemView.findViewById(R.id.moneyNow);
                donateCount = itemView.findViewById(R.id.donateCount);
                activityAt = itemView.findViewById(R.id.activityAt);
                donation = itemView.findViewById(R.id.donation);
            }
        }
    }
